//
//  PSSingleRowView.m
//  Btpin
//
//  Created by Vic on 2019/3/13.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PSSingleRowView.h"

@interface PSSingleRowView ()<UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSArray *itemDataArray;
    PublicCategoryElementModel *pickerSelectModel;
}
@end

@implementation PSSingleRowView

- (instancetype)initWithData:(NSArray<PublicCategoryElementModel *> *)dataArray target:(id)target {
    self = [super init];
    if (self) {
        itemDataArray = dataArray;
        pickerSelectModel = [[PublicCategoryElementModel alloc] init];
        pickerSelectModel = dataArray[0];
        [self initUI];
        self.delegate = target;
    }
    return self;
}
- (void)initUI {
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.backgroundColor = [UIColor colorWithWhite:0.01 alpha:0.5];
    [self addTarget:self action:@selector(cancelBtnTap) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, HEIGHT-SIMEmojKeyBoardkBottomNOSafeArea_Height-PickerHeight-50*Scale, WIDTH, PickerHeight+50*Scale) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:bgView];
    bgView.userInteractionEnabled = YES;
    
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, PickerHeight)];
    pickerView.backgroundColor = COLOR_OF_WHITE;
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [pickerView reloadAllComponents];
    [bgView addSubview:pickerView];
    
    
    UIButton *cancelBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, ViewHeight(bgView)-50*Scale, WIDTH*0.5, 50*Scale) imageName:nil target:self action:@selector(cancelBtnTap) title:@"取消" titleColor:THEMECOLOR_OF_TEXT_9a9a9a];
    cancelBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:cancelBtn];
    [cancelBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(cancelBtn), StartY(cancelBtn), ViewWidth(cancelBtn), ViewHeight(cancelBtn)) imageName:nil target:self action:@selector(okBtnTap) title:@"确认" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:okBtn];
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeLeft)];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50*Scale;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return itemDataArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    PublicCategoryElementModel *model = itemDataArray[row];
    NSString *title = model.name;
    return title;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    pickerSelectModel = itemDataArray[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews) {
        if (singleLine.frame.size.height < 1) {
            singleLine.backgroundColor = COLOR_OF_CLEAR;
        }
    }
    //设置文字的属性
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    genderLabel.textColor = THEMECOLOR_OF_TEXT_333333;
    genderLabel.font = Font(Font_17);
    genderLabel.backgroundColor = COLOR_OF_CLEAR;
    return genderLabel;
}
- (void)cancelBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psSingleRowViewCancel:)]) {
        [self.delegate psSingleRowViewCancel:self];
    }
}
- (void)okBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psSingleRowViewOk:PSSingleRowView:)]) {
        [self.delegate psSingleRowViewOk:pickerSelectModel PSSingleRowView:self];
    }
}

@end
